<?php
/**
 * 前台首页控制器。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Controllers;

use View;
use Cache;
use Config;
use App\Http\Model\Album;
use App\Http\Model\Banner;

class IndexController extends Shadow\ShadowController
{
    /**
     * 是否获取猜你喜欢模块内容。
     */
    const GET_LOVE = true;
    
    public function onGet()
    {
        $this->results['banners'] = $this->getBannerLists();
        $this->results['lists'] = $this->getIndexLists();
        $this->results['newlists'] = $this->getNewLists();
        
        return View::make('index', $this->results);
    }
    
    /**
     * 获取首页访问次数最多的作品并随机排序。
     * @return array
     */
    private function getIndexLists() 
    {
        $a_lists = array();
        if (Cache::has('index_lists') && Config::get('app.debug') == false) {
            $a_lists = json_decode(Cache::get('index_lists'), true);
            if (count($a_lists)) {
                shuffle($a_lists);
                
                return $a_lists;
            }
        } 
        $a_lists = $this->getAllLists();
        if (count($a_lists)) {
            $a_visit_times = array();
            foreach ($a_lists as $vv) {
                $a_visit_times[$vv['pa_id']] = $vv['visit_times'];
            }
            array_multisort($a_visit_times, SORT_DESC, SORT_NUMERIC, $a_lists);
            $a_lists = array_slice($a_lists, 0, 8);
            foreach ($a_lists as &$vv) {
                unset($vv['uid']);
                unset($vv['type_id']);
                unset($vv['status']);
                unset($vv['create_time']);
                unset($vv['description']);
                
                $s_cover = '';
                if (Config::get('app.env') == 'production') {
                    $s_cover = $this->getBaseUrl($vv['cover'] . '-cover.jpg');
                }
                $vv['cover'] = $s_cover;
            }
            if (Config::get('app.env') == 'production') {
                Cache::put('index_lists', json_encode($a_lists), static::CACHE_LIFETIME);
            }
        }
        shuffle($a_lists);
        
        return $a_lists;
    }
    
    private function getBannerLists()
    {
        $a_lists = array();
        if (Cache::has('banner_lists') && Config::get('app.debug') == false) {
            $a_lists = json_decode(Cache::get('banner_lists'), true);
            if (count($a_lists)) {
                shuffle($a_lists);
                $a_lists = array_slice($a_lists, 0, 6);
                
                return $a_lists;
            }
        } 
        $a_lists = Banner::where('status', '=', 'show')->get()->toArray();
        if (count($a_lists)) {
            foreach ($a_lists as &$vv) {
                unset($vv['vb_id']);
                unset($vv['status']);
                unset($vv['weight']);
                $vv['banner'] = '';                   
                if (Config::get('app.env') == 'production') {
                    $vv['banner'] = $this->getBaseUrl($vv['resource'] . '-banner.jpg');
                }
            }
            if (Config::get('app.env') == 'production') {
                Cache::put('banner_lists', json_encode($a_lists), static::CACHE_LIFETIME);
            }
            shuffle($a_lists);
            $a_lists = array_slice($a_lists, 0, 6);
        }
        
        return $a_lists;
    }
    
    private function getNewLists()
    {
        $a_list = array();
        if (Cache::has('new_lists') && Config::get('app.debug') == false) {
            $a_lists = json_decode(Cache::get('new_lists'), true);
            
            return $a_lists;
        }
        $a_lists = $this->getAllLists();
        if (!count($a_lists)) {
            return $a_lists;
        }
        $a_lists = array_slice($a_lists, count($a_lists) - 2, 2);
        if (count($a_lists) && Config::get('app.env') == 'production') {
            foreach ($a_lists as &$vv) {
                $vv['cover'] = $this->getBaseUrl($vv['cover'] . '-cover.jpg');
            }
            Cache::put('new_lists', json_encode($a_lists), static::CACHE_LIFETIME);
        }
        
        return $a_lists;
    }
}
